<template>
  <div></div>
</template>
<script>
  import {commonService} from '../../service/commonService'
  import {loginService} from '../../service/loginService'
  export default {
    props: [],
    data(){
      // document.title = '微信登录';
      return {
        GWC: {
          version: '1.1.1',
          urlParams: {},
        }
      }
    },
    mounted () {
      let that = this
      that.getUrlParams();
      that.doRedirect();
    },
    methods: {
      appendParams: function(url, params) {
        let that = this
        if (params) {
          var baseWithSearch = url.split('#')[0];
          var hash = url.split('#')[1];
          for (var key in params) {
            var attrValue = params[key];
            if (attrValue !== undefined) {
              var newParam = key + "=" + attrValue;
              if (baseWithSearch.indexOf('?') > 0) {
                var oldParamReg = new RegExp('^' + key + '=[-%.!~*\'\(\)\\w]*', 'g');
                if (oldParamReg.test(baseWithSearch)) {
                  baseWithSearch = baseWithSearch.replace(oldParamReg, newParam);
                } else {
                  baseWithSearch += "&" + newParam;
                }
              } else {
                baseWithSearch += "?" + newParam;
              }
            }
          }

          if (hash) {
            url = baseWithSearch + '#' + hash;
          } else {
            url = baseWithSearch;
          }
        }
        return url;
      },
      getUrlParams: function() {
        let that = this
        var pairs = location.search.substring(1).split('&');
        for (var i = 0; i < pairs.length; i++) {
          var pos = pairs[i].indexOf('=');
          if (pos === -1) {
            continue;
          }
          that.GWC.urlParams[pairs[i].substring(0, pos)] = decodeURIComponent(pairs[i].substring(pos + 1));
        }
      },
      doRedirect: function() {
        let that = this
        var code = that.GWC.urlParams['code'];
        var appId = that.GWC.urlParams['appid'];
        var scope = that.GWC.urlParams['scope'] || 'snsapi_base';
        var state = that.GWC.urlParams['state'];
        var redirectUri;

        if (!code) {
          //第一步，没有拿到code，跳转至微信授权页面获取code
          redirectUri = that.appendParams('https://open.weixin.qq.com/connect/oauth2/authorize#wechat_redirect', {
            'appid': appId,
            'redirect_uri': encodeURIComponent(location.href),
            'response_type': 'code',
            'scope': scope,
            'state': state,
          });
          location.href = redirectUri;//此处ajax发送请求
        } else {
          //第二步，从微信授权页面跳转回来，已经获取到了code，再次跳转到实际所需页面
          if(that.$route.query.plat=='front'){

          }else if(that.$route.query.plat=='admin'){
               location.href ="http://admin.welltour.cn/wxskip?code="+code+"&appId="+appId+'&scope='+scope+'&state='+state
                return false
          }

          // let platform = commonService.getPlatForm();
          // let statea = commonService.getState();
          loginService.getWXByCode({code: code,state: 'web',platform: 'WAPH5'}).then(function (res) {
            if(res.data.success){
              // that.$router.push({path: '/register'});
              if(res.data.code === 210101) { //登录成功请绑定手机号
                setTimeout(function () {
                   window.localStorage.setItem('redisInfoKey',res.data.datas.redisInfoKey)
                   if(localStorage.token!=''&&localStorage.token!=null){
                       commonService.getWXbind({redisInfoKey: res.data.datas.redisInfoKey,plat:'pc',platform: 'WAPH5'}).then(function (res) {
                            // if(res.data.code == 212102){
                            //   that.redisInfoKey = res.data.datas
                            //   that.$nextTick(function(){
                            //     that.open2()
                            //   })
                            // } else 
                            if(res.data.code == 200){
                              that.$router.push({name: 'setup'});
                            }
                        })

                   }else{
                    that.$router.push({path: '/binding'});
                   }
                },2000)
              }else if(res.data.code === 210100){ //登录成功  获取token
                localStorage.token = res.data.datas;
                  // that.$route.params.shopId = localStorage.pathId;
                  that.$router.replace({name: 'home'});
               
              }else{
                   that.$message.error(res.data.message);
                  that.$router.push({name: 'login'});
              }
            }else {
              /*if(res.data.code === 210101) {

              }*/
            }
          })
        }
      }
    }
  }
</script>
<style></style>